#pragma once

#ifdef __WIN32
#else
#include <sys/stat.h>
#include <sys/time.h>
#endif

#include <fstream>
#include <string>
#include <regex>
#include <filesystem>
namespace fs = std::filesystem;

namespace bm
{
namespace file
{
inline std::string read_file(const std::string& file_path)
{
    std::ifstream f(file_path);
    std::istreambuf_iterator<char> beg(f), end; 
    std::string str(beg, end);

    return str;
}
inline void write_file(const std::string& file_path, const std::string& content)
{
    std::fstream f(file_path, std::ios::in | std::ios::app);
    f << content;
}
}

}

namespace bm
{
namespace time
{
/*
* @brief  获取当前时间戳精确到ms
* @return 返回int64类型到时间戳
*/
inline int64_t get_current_timestamp()
{
    int64_t result = 0;
#ifdef __WIN32
    result = 0;
#else
    struct timeval tv;
    gettimeofday(&tv,NULL);
    result = tv.tv_sec * 1000 + tv.tv_usec / 1000;
#endif

    return result;
}
/*
* @brief  获取当前到日期
* @return 返回 格式是 yyyy-mm-dd HH:MM:SS
*/
inline std::string get_current_datetime()
{
    char c_time[50] = { 0 };
#ifdef __WIN32
#else
    time_t rawtime;
    struct tm *ptminfo;
    ::time(&rawtime);
    ptminfo = localtime(&rawtime);
    sprintf(c_time, "%02d-%02d-%02d %02d:%02d:%02d",
    ptminfo->tm_year + 1900, ptminfo->tm_mon + 1, ptminfo->tm_mday,
    ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec);
#endif
    return c_time;
}
/*
* @brief  获取当前日期
* @return 返回 格式是 yyyy-mm-dd
*/
inline std::string get_current_date()
{
    char c_time[50] = { 0 };
#ifdef __WIN32
#else
    time_t rawtime;
    struct tm *ptminfo;
    ::time(&rawtime);
    ptminfo = localtime(&rawtime);
    sprintf(c_time, "%02d-%02d-%02d",
    ptminfo->tm_year + 1900, ptminfo->tm_mon + 1, ptminfo->tm_mday);
#endif
    return c_time;
}
/*
* @brief  获取前一天的日期
* @return 返回 格式是 yyyy-mm-dd
*/
inline std::string get_pre_date()
{
    char c_time[50] = { 0 };
#ifdef __WIN32
#else
    time_t rawtime;
    struct tm *ptminfo;
    ::time(&rawtime);
    rawtime -= 24 * 60 * 60;
    ptminfo = localtime(&rawtime);
    sprintf(c_time, "%02d-%02d-%02d",
    ptminfo->tm_year + 1900, ptminfo->tm_mon + 1, ptminfo->tm_mday);
#endif
    return c_time;
}
/*
* @brief  获取当前时间
* @return 返回 格式是 HH:MM:SS
*/
inline std::string get_current_time()
{
    char c_time[50] = { 0 };
#ifdef __WIN32
#else
    time_t rawtime;
    struct tm *ptminfo;
    ::time(&rawtime);
    ptminfo = localtime(&rawtime);
    sprintf(c_time, "%02d:%02d:%02d",
    ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec);
#endif
    return c_time;
}

}
}
